<template>
	<el-card class="form-container">
		<el-form :model="dataForm" ref="dataForm" :rules="dataRule" @keyup.enter.native="dataFormSubmitHandle()" label-width="90px">
			<el-form-item label="分类名称" prop="categoryName">
				<el-input v-model="dataForm.categoryName" placeholder="请输入分类名称"></el-input>
			</el-form-item>
			<el-form-item label="上级分类" prop="categoryParentCode">
				<treeselect
					v-model="dataForm.categoryParentCode"
					:options="categoryOptions"
					:normalizer="normalizer"
					:show-count="true"
					placeholder="选择上级分类"
				/>
			</el-form-item>
			<el-form-item label="分类图标" prop="categoryIcon">
				<el-upload
					class="avatar-uploader"
					action="#"
					:show-file-list="false"
					:on-success="handleAvatarSuccess"
					:before-upload="beforeAvatarUpload"
					:auto-upload="true"
					:data="categoryImage"
					:http-request="uploadImage">
					<img v-if="dataForm.categoryIcon" :src="dataForm.categoryIcon" class="avatar">
					<i v-else class="el-icon-plus avatar-uploader-icon"></i>
				</el-upload>
			</el-form-item>
			<el-form-item label="分类级别" prop="categoryLevel">
				<el-radio-group v-model="dataForm.categoryLevel">
					<el-radio label="1">一级分类</el-radio>
					<el-radio label="2">二级分类</el-radio>
					<el-radio label="3">三级分类</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="类目排序" prop="categorySort">
				<el-input v-model="dataForm.categorySort" placeholder="请输入类目排序"></el-input>
			</el-form-item>
			<el-form-item label="类目状态" prop="categoryStatus">
				<el-radio-group v-model="dataForm.categoryStatus">
					<el-radio :label="1">启用</el-radio>
					<el-radio :label="2">禁用</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="模版编号" prop="templateCode">
				<el-input v-model="dataForm.templateCode" placeholder="请输入模版编号"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button @click="cancel">取消</el-button>
				<el-button type="primary" @click="submit('dataForm')">确定</el-button>
			</el-form-item>
		</el-form>
	</el-card>
</template>

<script>
	import { addGoodsCategory ,findCategoryTree} from '@/api/mall/goodsCategory'
	import Treeselect from "@riophae/vue-treeselect";
	import "@riophae/vue-treeselect/dist/vue-treeselect.css";
	import { uploadImage } from '@/api/common/upload'
	export default {
		name: 'add-goodsCategory',
		data() {
			return {
				categoryOptions:[],
				dataForm: {
					categoryName: '',
					categoryParentCode: null,
					categoryIcon: '',
					categoryLevel: '',
					categorySort: '',
					categoryStatus: 1,
					createTime: '',
					updateTime: '',
					categoryCode: '',
					templateCode: '',
				},
				categoryImage:{
					uploadType:'eyeglass-user'
				},
				dataRule: {
					categoryName: [{
							required: true,
							message: '请输入分类名称',
							trigger: 'blur'
						},
					],
				}
			}
		},
		components:{
			Treeselect
		},
		created(){
			//初始化菜单树
			this.initTreeList();
		},
		methods: {
			initTreeList(){
				findCategoryTree().then(response => {
					console.log(response)
					this.categoryOptions = [];
					const category = { categoryCode: 0, categoryName: '主类目', children: [] };
					category.children = this.handleTree(response.data.responseData, "categoryCode");
					console.log(category)
					this.categoryOptions.push(category);
				})
			},
			/** 转换菜单数据结构 */
			normalizer(node) {
				if (node.children && !node.children.length) {
					delete node.children;
				}
				return {
					id: node.categoryCode,
					label: node.categoryName,
					children: node.children
				};
			},
			cancel() {
				this.$router.go(-1)
			},
			submit() {
				const set = this.$refs;
				set['dataForm'].validate((valid) => {
					if (!valid) {
						return false
					}
					addGoodsCategory(this.dataForm).then(() => {
						this.$notify({
							title: '添加成功',
							message: '添加成功',
							type: 'success',
							duration: 2000,
							onClose: this.$router.go(-1)
						})
					})
				})
			},
			beforeAvatarUpload(file) {
				const isJPG = file.type === 'image/jpeg';
				const isLt2M = file.size / 1024 / 1024 < 2;
				if (!isJPG) {
					this.$message.error('上传头像图片只能是 JPG 格式!');
				}
				if (!isLt2M) {
					this.$message.error('上传头像图片大小不能超过 2MB!');
				}
				return isJPG && isLt2M;
			},
			handleAvatarSuccess(res, file) {
				this.dataForm.categoryIcon = URL.createObjectURL(file.raw);
			},
			uploadImage(val){
				let formData = new FormData();
				formData.append('uploadType',this.categoryImage.uploadType)
				formData.append('files',val.file)
				uploadImage(formData).then(res =>{
					this.dataForm.categoryIcon = res.data.responseData[0]
				})
			}
		}
	}
</script>

<style>
.avatar-uploader .el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
}
.avatar-uploader .el-upload:hover {
	border-color: #409EFF;
}
.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 80px;
	height: 80px;
	line-height: 80px;
	text-align: center;
}
.avatar {
	width: 80px;
	height: 80px;
	display: block;
}
</style>
